// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 2008-7-29 12:58:11
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   StringUtil.java

package com.creawor.imei.util;

import java.text.DecimalFormat;
import java.util.List;

// Referenced classes of package com.creawor.imei.util:
//            StringList

public class StringUtil {
	private static DecimalFormat formatter = new DecimalFormat("00.00");

	public StringUtil() {
	}

	public static String getFrontStr(String str, String token) {
		int pos1 = str.lastIndexOf(token);
		if (pos1 >= 0)
			return str.substring(0, pos1);
		else
			return str;
	}

	public static String getBackStr(String str, String token) {
		int pos1 = str.lastIndexOf(token);
		if (pos1 >= 0)
			return str.substring(pos1 + 1);
		else
			return str;
	}

	public static String deleteLastChar(String src) {
		if (src == null || src.length() <= 1)
			return null;
		else
			return src.substring(0, src.length() - 1);
	}

	public static void deleteLastChar(StringBuffer sb) {
		deleteLastChar(sb, 1);
	}

	public static void deleteLastChar(StringBuffer sb, int len) {
		if (sb == null || sb.length() <= len) {
			return;
		} else {
			sb.delete(sb.length() - len, sb.length());
			return;
		}
	}

	public static void addToList(List lst, Object obj) {
		if (obj instanceof String) {
			String strValue = (String) obj;
			for (int i = 0; i < lst.size(); i++) {
				String value = (String) lst.get(i);
				if (value.equals(strValue))
					return;
			}

			lst.add(obj);
		}
	}

	public static void addToStringList(StringList lst, String str) {
		for (int i = 0; i < lst.size(); i++)
			if (lst.get(i).equals(str))
				return;

		lst.add(str);
	}

	public static String lstToSting(List lst, String token) {
		if (lst == null || token == null)
			return null;
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < lst.size(); i++) {
			String value = (String) lst.get(i);
			sb.append((new StringBuilder(String.valueOf(value))).append(token)
					.toString());
		}

		deleteLastChar(sb, token.length());
		return sb.toString();
	}

	public static String convertStringToNull(String anObject) {
		if ("".equals(anObject) || anObject.equalsIgnoreCase("null"))
			return null;
		else
			return anObject;
	}

	public static String valueOf(String obj) {
		if (obj == null || obj.equalsIgnoreCase("null"))
			return "";
		else
			return obj;
	}

	public static String lstToValueStr(List lst, String key) {
		String str = "";
		if (lst == null)
			return null;
		for (int i = 0; i < lst.size(); i++)
			str = (new StringBuilder(String.valueOf(str))).append("&").append(key)
					.append("=").append((String) lst.get(i)).toString();

		return str;
	}

	public static String[] lstToStringArr(List lst) {
		if (lst == null)
			return null;
		String arr[] = new String[lst.size()];
		for (int i = 0; i < lst.size(); i++)
			arr[i] = lst.get(i).toString();

		return arr;
	}

	public static String toUtf8String(String s) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c >= 0 && c <= '\377') {
				sb.append(c);
			} else {
				byte b[];
				try {
					b = String.valueOf(c).getBytes("utf-8");
				} catch (Exception ex) {
					//System.out.println(ex);
					b = new byte[0];
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					sb.append((new StringBuilder("%")).append(
							Integer.toHexString(k).toUpperCase()).toString());
				}

			}
		}

		return sb.toString();
	}

	public static String format(double input) {

		return formatter.format(input);
	}

	public static String format(float input) {

		return formatter.format(input);
	}

	public static String formatDouble(String input) {
		if (input == null) {
			return "0";
		}
		return formatter.format(Double.valueOf(input));
	}

	public static String filter(String value) {

		if (value == null)
			return (null);

		StringBuffer result = new StringBuffer();
		for (int i = 0; i < value.length(); i++) {
			char ch = value.charAt(i);
			if (ch == '<')
				result.append("&lt;");
			else if (ch == '>')
				result.append("&gt;");
			else if (ch == '&')
				result.append("&amp;");
			else if (ch == '"')
				result.append("&quot;");
			else if (ch == '\r')
				result.append("<BR>");
			else if (ch == '\n') {
				if (i > 0 && value.charAt(i - 1) == '\r') {

				} else {
					result.append("<BR>");
				}
			} else if (ch == '\t')
				result.append("&nbsp;&nbsp;&nbsp;&nbsp");
			else if (ch == ' ')
				result.append("&nbsp;");
			else
				result.append(ch);
		}
		return (result.toString());
	}
	
	public static boolean isIn(String str,String[] strs){
		boolean flg=false;
		if(null!=str && null!=strs){
			String tmp=null;
			for(int i=0;i<strs.length;i++){
				tmp=strs[i];
				if(str.equalsIgnoreCase(tmp))flg=true;
			}
		}
		return flg;
	}

	public static void main(String[] args) {
		double input = 123455666788908888d;
		//System.out.println(format(input));
		String input2 = "123455666788908888";
		
		String str=java.net.URLEncoder.encode("乐昌");
		System.out.println(str);
		//System.out.println(formatDouble(input2));
		//System.out.println("huiche:" + System.getProperty("line.separator"));
	}
}